<template>
  <el-form
    style="margin-top: -12px"
    v-loading="loading"
    :model="ruleForm"
    status-icon
    :rules="rules"
    ref="ruleForm"
    label-width="100px"
    class="demo-ruleForm">
    <el-form-item label="呢称" prop="name">
      <el-input  v-model="ruleForm.name" ></el-input>
    </el-form-item>
    <el-form-item label="出生日期" prop="birthday">
      <el-date-picker v-model="ruleForm.birthday"
                      type="date" placeholder="选择日期"
                      style="width: 320px"
                      format="YYYY 年 MM 月 DD 日"
                      value-format="YYYY-MM-DD"></el-date-picker>
    </el-form-item>
    <el-form-item label="性别" prop="sex">
      <el-radio v-model="ruleForm.sex" :label="1">男</el-radio>
      <el-radio v-model="ruleForm.sex" :label="2">女</el-radio>
    </el-form-item>
    <el-form-item label="手机号码" prop="phone">
      <el-input  v-model="ruleForm.phone" ></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input  v-model="ruleForm.email" ></el-input>
    </el-form-item>
    <el-form-item label="部门" prop="deptId">
      <el-cascader
        style="width: 320px"
        v-model="ruleForm.deptId"
        @change="change"
        :options="deptList"
        :props="{ checkStrictly: true ,children: 'children',label:'deptName',value:'id'}"
        clearable></el-cascader>
    </el-form-item>
    <el-form-item label="岗位" prop="postId">
      <el-select v-model="ruleForm.postId" placeholder="请选择" style="width: 320px">
        <el-option
          v-for="item in postList"
          :key="item.id"
          :label="item.postName"
          :value="item.id"
        >
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="角色" prop="roleId">
      <el-select  multiple v-model="auxiliary.roleArray" placeholder="请选择" style="width: 320px" @change="changeRole">
        <el-option
          v-for="item in roleList"
          :key="item.id"
          :label="item.roleName"
          :value="item.id"
        >
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-select v-model="ruleForm.status" placeholder="请选择" style="width: 320px">
        <el-option
          label="启用"
          :value="1">
        </el-option>
        <el-option
          label="启用"
          :value="1">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item style="margin-top: 10px;margin-left: -28px">
      <el-button type="primary" @click="submitForm('ruleForm')" style="width: 320px;height: 31px;">提交</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { ElMessage } from 'element-plus'

export default {
  name: 'DataUpdate',
  //组件参数
  props:{
    propsData:{
      type:Object,
      required:true
    },
    deptTree:{
      type:Array,
      required:true
    }
  },
  data(){
    return{
      auxiliary:{
        roleArray:[]
      },
      ruleForm:{},
      loading:false,
      rules:{
        name: [
          { required: true, message: '请输入呢称', trigger: 'blur' },
        ],
        birthday: [
          { required: true, message: '请选择出生日期', trigger: 'blur' },
        ],
        sex: [
          { required: true, message: '请选中性别', trigger: 'blur' },
        ],
        phone: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
        ],
        deptId: [
          { required: true, message: '请选择部门信息', trigger: 'blur' },
        ],
        postId: [
          { required: true, message: '请选择岗位信息', trigger: 'blur' },
        ],
        roleId: [
          { required: true, message: '请选择角色信息', trigger: 'blur' },
        ],
        status: [
          { required: true, message: '请选择状态', trigger: 'blur' },
        ],
      },
      roleList:[],
      deptList:[],
      postList:[]
    }
  },
  mounted () {
    //转换数据，因为props中的属性是不可以修改的
    this.ruleForm = JSON.parse(JSON.stringify(this.propsData))
    this.getPost();
    this.getDept();
    this.getRole();
    this.auxiliary.roleArray = this.ruleForm.roleId.split(',')
  },
  methods:{
    //提交数据
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          this.$http.post('/api/user/update',this.ruleForm).then(res => {
            this.loading = false
            ElMessage({
              type: 'success',
              showClose: true,
              message: res.data.message,
            })
            //触发事件
            this.$emit('tao')
          }).catch(() => this.loading = false)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    //部门类型级联选择器，获取的的全路径，这里只需要最后一级
    change(val){
      //拼接路径
      this.ruleForm.deptId = val[val.length - 1]
    },
    //角色下拉选择
    changeRole(val){
      if (val.length !== 0){
        this.ruleForm.roleId = val.reduce((previousValue, currentValue) => previousValue + "," + currentValue)
      }
    },
    //获得部门信息
    getDept(){
      this.$http.get('/api/dept/tree').then(res =>{
        this.deptList = res.data.data
      })
    },
    //获得岗位信息
    getPost(){
      this.$http.get('/api/post',{params:{ current: 1, size: 100}}).then(res =>{
        this.postList = res.data.data.records
      })
    },
    //获得角色数据
    getRole(){
      this.$http.get('/api/role',{params:{ current: 1, size: 100}}).then(res =>{
        this.roleList = res.data.data.records
      })
    },
  }
}
</script>

<style scoped>

</style>
